package org.i1510.common.aspect;

import java.lang.reflect.Method;
import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.i1510.common.enums.Header;
import org.i1510.common.utils.LogTools;
import org.i1510.common.utils.ParamValidation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ServletContextAware;


@Aspect
public class LogAspectService implements ServletContextAware {
	private static Logger logger = LoggerFactory.getLogger(LogAspectService.class);

	private final static String rm = "@annotation(org.springframework.web.bind.annotation.RequestMapping)";
	private ServletContext ctx;
	@Around(rm)
	public Object service(ProceedingJoinPoint call) throws Throwable {

		Object object = null;
		Object[] os = call.getArgs();
		Signature signature = call.getSignature();
		MethodSignature methodSignature = (MethodSignature) signature;
		Method method = methodSignature.getMethod();
		for (Object o : os) {
			if (o instanceof HttpServletRequest) {
				HttpServletRequest requests = (HttpServletRequest) o;
				String userid = "";
				String ip = requests.getRemoteAddr();
				String action = method.getName();
				String version = requests.getHeader(Header.VERSION.getName());
				String platform = requests.getHeader(Header.PLATFORM.getName());
				String channel = requests.getHeader(Header.CHANNEL.getName());
				String network = requests.getHeader(Header.NETWORK.getName());
				String msg = requests.getHeader(Header.MSG.getName());
				String mac = requests.getHeader(Header.MAC.getName());
				String mobilenum = requests.getHeader(Header.CMWAPMOBILENUM.getName());
				if (StringUtils.isEmpty(mobilenum)) {
					mobilenum = requests.getHeader(Header.MOBILENUM.getName());
				}
				
				if(StringUtils.isEmpty(mobilenum)) {
					mobilenum = "无法获取手机号";
				}
				
				LogTools log = new LogTools(logger, ip, action, "18030181805", mac, msg, userid);

				// 判断头信息是否存在
				/*if (StringUtils.isEmpty(network)) {
					return log.error(100101, Header.NETWORK.getName());
				}
				// 暂时屏蔽手机号验证
//				if (StringUtils.isEmpty(mobilenum)) {
//					return this.error(100102, Header.MOBILENUM.getName());
//				}
				if (StringUtils.isEmpty(platform)) {
					return log.error(100103, Header.PLATFORM.getName());
				}
				if (StringUtils.isEmpty(version)) {
					return log.error(100104, Header.VERSION.getName());
				}
				if (StringUtils.isEmpty(channel)) {
					return log.error(100105, Header.CHANNEL.getName());
				}
				if (StringUtils.isEmpty(mac)) {
					return log.error(100106, Header.MAC.getName());
				}

				// 鉴权
				if (!method.isAnnotationPresent(NotRequireAuth.class)) {
					if (StringUtils.isEmpty(msg)) {
						return log.error(100107, Header.MSG.getName());
					} else {
						// 判断msg是否正确
						userid = msg;
						log.setUserid(userid);
					}
				}*/

				// 接口参数效验
				if (method.isAnnotationPresent(ParamValidation.class)) {
					String[] params = method.getAnnotation(ParamValidation.class).value();
					for (String param : params) {
						if (StringUtils.isEmpty(requests.getParameter(param))) {
							return log.error(100108, param);
						}
					}
				}

				// 输出所有参数
				log.info("/*===========================REQUEST============================*/");
				log.info("/* request = " + requests.getServletPath());
				log.info("/*----------------------------HEADER----------------------------*/");
				Enumeration<?> pl = requests.getHeaderNames();
				while (pl.hasMoreElements()) {
					String key = pl.nextElement().toString();
					String value = requests.getHeader(key);
					log.info("/* " + key + " = " + value);
				}
				log.info("/*-----------------------------BODY-----------------------------*/");

				Enumeration<?> pa = requests.getParameterNames();
				while (pa.hasMoreElements()) {
					String key = pa.nextElement().toString();
					String value = requests.getParameter(key);
					log.info("/* " + key + " = " + value);
				}
				object = call.proceed();
				log.info("/*---------------------------RESPONSE---------------------------*/");
				log.info("/* response = " + object.toString());

			}
		}
		return object;
	}

	@Override
	public void setServletContext(ServletContext servletContext) {
		// TODO Auto-generated method stub
		this.ctx = servletContext;
	}
}
